package com.parleys.activity;

import java.util.List;

import roboguice.inject.InjectExtra;
import roboguice.inject.InjectView;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.inject.Inject;
import com.parleys.R;
import com.parleys.androidservice.ParleysMediaPlayer;
import com.parleys.domain.Channel;
import com.parleys.domain.Presentation;
import com.parleys.domain.Space;
import com.parleys.domain.Speaker;
import com.parleys.helper.ApplicationConstants;
import com.parleys.helper.DownloadTask;
import com.parleys.service.intf.ParleysService;
import com.parleys.service.intf.PresentationAssetsService;

/**
 * Displays the details (name, description, speakers, ...) of a presentation. The user can navigate to the chapters or
 * slides using the menu.
 * 
 * @author David Van Droogenbroeck
 * 
 */
public class PresentationDetailActivity extends AbstractParleysActivity {

	private static final String TAG = PresentationDetailActivity.class.getName();

	@InjectExtra(ApplicationConstants.SPACE_ARG)
	private Space space;

	@InjectExtra(ApplicationConstants.CHANNEL_ARG)
	private Channel channel;

	@InjectExtra(ApplicationConstants.ONLINE_ARG)
	private Boolean online;

	@InjectExtra(ApplicationConstants.PRESENTATION_ARG)
	private Presentation presentation;

	@Inject
	private ParleysService parleysService;

	@Inject
	private PresentationAssetsService presentationAssetsService;

	@InjectView(R.id.play_presentation)
	private ImageButton playButton;

	@InjectView(R.id.presentation_description)
	private TextView presentationDescriptionTextView;

	@InjectView(R.id.image)
	private ImageView imageView;

	@InjectView(R.id.speakers_name)
	private TextView speakersNameTextView;

	@InjectView(R.id.speakers_description)
	TextView speakersDescriptionTextView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.presentation_detail);
		setTitle(presentation.getName());

		presentationDescriptionTextView.setText(presentation.getDescription());

		List<Speaker> speakers = presentation.getSpeakers();
		StringBuilder speakersBio = new StringBuilder(4096);

		for (Speaker speaker : speakers) {
			if (speakersBio.length() != 0) {
				speakersBio.append("\n\n");
			}
			speakersBio.append(speaker.getBio());
		}

		speakersNameTextView.setText(presentation.getSpeakersString());
		speakersDescriptionTextView.setText(speakersBio.toString());
		imageView.setImageBitmap(presentationAssetsService.getImage(channel.getAssetBaseURL(), "presentations",
				presentation.getId(), presentation.getThumbnail()));

		playButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ParleysMediaPlayer.toggle(channel, presentation, online);
				if (ParleysMediaPlayer.isPlaying()) {
					playButton.setImageResource(android.R.drawable.ic_media_pause);
					gotoSlidesActivity(space, channel, presentation, online);
				} else {
					playButton.setImageResource(android.R.drawable.ic_media_play);
				}
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (ParleysMediaPlayer.isPlaying()) {
			playButton.setImageResource(android.R.drawable.ic_media_pause);
		} else {
			playButton.setImageResource(android.R.drawable.ic_media_play);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		if (online) {
			MenuItem previewChaptersMenuItem = menu.add(0, 0, 0, getString(R.string.preview_chapters));
			previewChaptersMenuItem.setIcon(android.R.drawable.ic_media_play);
			MenuItem previewSlidesMenuItem = menu.add(1, 1, 0, getString(R.string.preview_slides));
			previewSlidesMenuItem.setIcon(android.R.drawable.ic_menu_gallery);
			MenuItem downloadMenuItem = menu.add(2, 2, 0, getString(R.string.download));
			downloadMenuItem.setIcon(android.R.drawable.ic_menu_save);

			if (parleysService.isPresentationDownloaded(presentation.getId())) {
				menu.setGroupEnabled(2, false);
			}

		} else {
			MenuItem chaptersMenuItem = menu.add(0, 0, 0, getString(R.string.chapters));
			chaptersMenuItem.setIcon(android.R.drawable.ic_media_play);
			MenuItem slidesMenuItem = menu.add(1, 1, 0, getString(R.string.slides));
			slidesMenuItem.setIcon(android.R.drawable.ic_menu_gallery);
		}
		return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			Log.d(TAG, "First menu button pressed");
			gotoChaptersActivity(space, channel, presentation, online);
			return true;
		case 1:
			Log.d(TAG, "Second menu button pressed");
			gotoSlidesActivity(space, channel, presentation, online);
			return true;
		case 2:
			Log.d(TAG, "Third menu button preseed");
			DownloadTask task = new DownloadTask(presentation, channel, space);
			getInjector().injectMembers(task);
			task.start();
			return true;
		}
		return false;
	}

}
